package com.gyf.szcrm.web.controller;

import com.gyf.szcrm.model.CURDResult;
import com.gyf.szcrm.model.CourseCategory;
import com.gyf.szcrm.model.CourseOrder;
import com.gyf.szcrm.model.PageResult;
import com.gyf.szcrm.service.ICourseCategoryService;
import com.gyf.szcrm.service.ICourseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/courseorder")
public class CourseOrderController {

    @Autowired
    ICourseOrderService orderService;

    @Autowired
    ICourseCategoryService categoryService;

    @RequestMapping("edit")
    public String edit(Model model,String order_id) {
        CourseOrder order = orderService.findByOrderId(order_id);
        model.addAttribute("order",order);
        List<CourseCategory> allList = categoryService.findAllList();
        model.addAttribute("category",allList);
        System.out.println(order_id);
        return "/courseorder/edit";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public CURDResult Delete(String order_id) {
       CURDResult result = new CURDResult();
        orderService.deleteByOrderId(order_id);
        return result;
    }

    @RequestMapping("/detail")
    public String Detail(Model model, String order_id) {
        CourseOrder order = orderService.findByOrderId(order_id);
        model.addAttribute("order",order);
        return "/courseorder/detail";
    }

    @RequestMapping("save")
    @ResponseBody
    public CURDResult save(CourseOrder order){
        System.out.println(order);
        CURDResult result = new CURDResult();
        if (order.getOrder_id() == null || order.getOrder_id().length()==0){
            //代表要插数据
            orderService.save(order);
        }else{
            //更新数据
            orderService.update(order);
        }

        return result;
    }

    @RequestMapping("/list")
    public String List() {
        return "/courseorder/list";
    }

    @RequestMapping("/add")
    public String Add(Model model) {
        //读取数据中的课程类别数据，供我们的页面使用
        List<CourseCategory> allList = categoryService.findAllList();
        model.addAttribute("category",allList);
        return "/courseorder/add";
    }

    @RequestMapping("listJson")
    //自动转成json数据
    @ResponseBody
    /**
     * page 显示当前页
     * limit每次显示多少条
     */
    public PageResult<CourseOrder> listJson(CourseOrder condition,int page,int limit){
        System.out.println(condition);
        PageResult<CourseOrder> result = orderService.findPageResult(condition,page,limit);//limit是pagesize
        return result;
    }


}
